package com.handydata.antifraud.service.impl;

import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.handydata.antifraud.bo.orderreturninfo.OrderReturnInfoBeanBO;
import com.handydata.antifraud.bo.orderreturninfo.OrderReturnInfoReqBO;
import com.handydata.antifraud.bo.orderreturninfo.OrderReturnInfoResBO;
import com.handydata.antifraud.common.exception.OrderReturnInfoExCode;
import com.handydata.antifraud.dao.OrderReturnInfoDao;
import com.handydata.antifraud.entity.OrderReturnInfoDO;
import com.handydata.antifraud.service.OrderReturnInfoService;
import com.handydata.common.convert.CommonBeanConvertor;
import com.handydata.common.exception.SysException;
import com.handydata.common.service.AbsService;

@Service("orderReturnInfoService")
public class OrderReturnInfoServiceImpl extends AbsService implements OrderReturnInfoService {
	
	private static final Logger logger = LoggerFactory.getLogger(OrderReturnInfoServiceImpl.class);
	
	@Autowired
	private OrderReturnInfoDao orderReturnInfoDao;

	@Transactional
	@Override
	public OrderReturnInfoResBO createOrderReturnInfo(OrderReturnInfoReqBO req) throws SysException{
	    OrderReturnInfoResBO res = new OrderReturnInfoResBO();
		try {
			OrderReturnInfoDO entity = CommonBeanConvertor.convert(req, OrderReturnInfoDO.class);
			entity.setId(UUID.randomUUID().toString().replace("-", ""));	
			long status = orderReturnInfoDao.insert(entity);
			if(status == 0) {
				res.setExCode(OrderReturnInfoExCode.SYS_NO_ENTITY_CREATED);
			} else {
				res.setExCode(OrderReturnInfoExCode.SUCCESS);
			}
 			
		} catch (Exception e) {
			logger.error("Create new entity {} get into error", req, e);
			throw new SysException(OrderReturnInfoExCode.SYS_CREATE_ENTITY_ERROR);
		}
		
		return res;
	}

	@Transactional
	@Override
	public OrderReturnInfoResBO modifyOrderReturnInfo(OrderReturnInfoReqBO req) throws SysException{
	    OrderReturnInfoResBO res = new OrderReturnInfoResBO();
		try {
			OrderReturnInfoDO condition = CommonBeanConvertor.convert(req, OrderReturnInfoDO.class);
			long status = orderReturnInfoDao.update(condition);
			if(status == 0) {
				res.setExCode(OrderReturnInfoExCode.SYS_NO_ENTITY_MODIFIED);
			} else {
				res.setExCode(OrderReturnInfoExCode.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("Modify entity {} get into error", req, e);
			throw new SysException(OrderReturnInfoExCode.SYS_MODIFY_ENTITY_ERROR);
		} 
		return res;
	}

	@Transactional
	@Override
	public OrderReturnInfoResBO deleteOrderReturnInfo(OrderReturnInfoReqBO req) throws SysException{
	    OrderReturnInfoResBO res = new OrderReturnInfoResBO();
		
		try {
			OrderReturnInfoDO condition = CommonBeanConvertor.convert(req, OrderReturnInfoDO.class);
			long status = orderReturnInfoDao.delete(condition);
			if(status == 0) {
				res.setExCode(OrderReturnInfoExCode.SYS_NO_ENTITY_DELETED);
			} else {
				res.setExCode(OrderReturnInfoExCode.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("Delete entity {} get into error", req, e);
			throw new SysException(OrderReturnInfoExCode.SYS_DELETE_ENTITY_ERROR);
		}
		return res;
	}

	@Override
	public OrderReturnInfoResBO queryOrderReturnInfo(OrderReturnInfoReqBO req) throws SysException{
		OrderReturnInfoDO condition = CommonBeanConvertor.convert(req, OrderReturnInfoDO.class);
		OrderReturnInfoResBO res;
		
		try {
			List<OrderReturnInfoDO> entities = orderReturnInfoDao.query(condition);
			res = CommonBeanConvertor.convert(entities, OrderReturnInfoResBO.class, OrderReturnInfoBeanBO.class);
		} catch(Exception e) {
			logger.error("Query entity {} get into error", req, e);
			throw new SysException(OrderReturnInfoExCode.SYS_QUERY_ENTITY_ERROR);
		}
		return res;
	}
	
}
